package service

import (
	"context"
	"disk/disk_rpc_server/disk_pb"
	"disk/utils/mysql"
	"gorm.io/gorm"
)

func (*GrpcServer) Delete(ctx context.Context, req *disk_pb.DeleteReq) (resp *disk_pb.DeleteResp, err error) {
	db := Database.Session(&gorm.Session{NewDB: true})
	var dataList []mysql.DiskList
	id := req.GetId()

	var tmpDataList []mysql.DiskList

	db.Where("id = ?", id).Find(&tmpDataList)
	if len(tmpDataList) == 0 {
		return &disk_pb.DeleteResp{
			Code:    1,
			Message: "删除失败，数据不存在",
		}, nil
	} else {
		result := db.Where("id = ?", id).Find(&dataList)
		if result.RowsAffected == 0 {
			return &disk_pb.DeleteResp{
				Code:    1,
				Message: "删除失败",
			}, nil
		} else if result.Error != nil {
			return &disk_pb.DeleteResp{
				Code:    1,
				Message: "删除失败" + result.Error.Error(),
			}, nil
		} else {
			return &disk_pb.DeleteResp{
				Code:    0,
				Message: "删除成功",
			}, nil
		}
	}
}
